//
//  WifiModule.m
//  baseapp
//
//  Created by dev on 2020/5/22.
//  Copyright © 2020 Facebook. All rights reserved.
//
#import <NetworkExtension/NetworkExtension.h>
#import <SystemConfiguration/CaptiveNetwork.h>
#import <Foundation/Foundation.h>
#import "GranwinAPKit.h"
#import "WifiModule.h"

@implementation WifiModule

@synthesize bridge = _bridge;

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(connWifi:(NSString *) appid :(NSString *) appsecert :(NSString *)ap_ssid :(NSString *) ap_ssid_pwd resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
   RCTLogInfo(@"==connWifi=== appid:%@,appsecert:%@,ap_ssid:%@,ap_ssid_pwd:%@", appid,appsecert,ap_ssid,ap_ssid_pwd);
  
    [[GranwinAPKit shared] start];
    [[GranwinAPKit shared] connectDeviceHot:ap_ssid hotPassword:ap_ssid_pwd didFinish:[GWCallBack onSuccessful:^(id result) {
       NSLog(@"===连接热点成功：%@", result);
       resolve(@"0000");
     } onFailed:^(NSError *err) {
        NSLog(@"===连接热点失败：%@", err);
       reject(@"0001", @"0001", err);
    }]];

//  [[GranwinAPKit shared] start];
  
//  [[GranwinAPKit shared] connectDeviceHot:ap_ssid hotPassword:ap_ssid_pwd didFinish:[GWCallBack onSuccessful:^(id result) {
//     NSLog(@"连接热点成功：%@", result);
//    [[GranwinAPKit shared] setDeviceNetwork:ssid wifiPassword:ssid_pwd timeoutSec:10 didFinish:[GWCallBack <GWDevice *> onSuccessful:^(GWDevice *result) {
//           NSLog(@"配网成功：%@", result);
//           [[GranwinAPKit shared] stopSetDeviceNetwork];
//       } onFailed:^(NSError *err) {
//            NSLog(@"配网失败：%@", err);
//       }]];
//   } onFailed:^(NSError *err) {
//      NSLog(@"连接热点失败：%@", err);
//  }]];
  
  // [[GranwinAPKit shared] stopSetDeviceNetwork]
  
  
}

RCT_EXPORT_METHOD(connWifi2:(NSString *) ssid  :(NSString *) ssid_pwd  resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
  
   RCTLogInfo(@"==connWifi2=== ssid:%@,ssid_pwd:%@",ssid,ssid_pwd);
    [[GranwinAPKit shared] setDeviceNetwork:ssid wifiPassword:ssid_pwd timeoutSec:10 didFinish:[GWCallBack <GWDevice *> onSuccessful:^(GWDevice *result) {
      NSLog(@"===配网成功：%@", result);
            resolve(result.mac);
            [[GranwinAPKit shared] stopSetDeviceNetwork];
         } onFailed:^(NSError *err) {
            NSLog(@"===配网失败：%@", err);
            reject(@"0001", @"0001", err);
    }]];
  
}

RCT_REMAP_METHOD(getSSID,resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
    
   
    NSString *kSSID = (NSString*) kCNNetworkInfoKeySSID;
    RCTLogInfo(@"==getSSID===%@",kSSID);
    NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
    for (NSString *ifnam in ifs) {
        NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
        if (info[kSSID]) {
            resolve(info[kSSID]);
            return;
        }
    }
    reject(@"cannot_detect_ssid", @"Cannot detect SSID", nil);
}

 

RCT_EXPORT_METHOD(setWifi) {
  
  
//  NSString * urlString = @"App-Prefs:root=WIFI";
//  if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]]) {
//      if ([[UIDevice currentDevice].systemVersion doubleValue] >= 10.0) {
//          [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];
//      } else {
//          [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
//      }
//  }
  
  
  
  NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

  if ([[UIApplication sharedApplication] canOpenURL:url]) {

      [[UIApplication sharedApplication] openURL:url];
  }
  
}






@end
